$(document).ready(function () {
    //注册表单验证
    $("register-from").validate({
        rules:{
            username:{
                required:true,
                rangelength:[5,10]
            },
            password:{
                required:true,
                rangelength:[5,10]
            },
            repassword:{
                required:true,
                rangelength:[5,10],
                equalTo:"#register-password"
            }
        },
        messages:{
            username:{
                required:"请输入用户名",
                rangelength:"用户名必须是5-10位"
            },
            password:{
                required:"请输入密码",
                rangelength:"密码必须是5-10位"
            },
            repassword:{
                required:"请确认密码",
                rangelength:"密码必须是5-10位",
                equalTo:"两次输入的密码必须相等"
            }
        },
        submitHandler:function (form) {
            var urlStr = "/register";
            // alert("urlStr:"+urlStr)
            $(form).ajaxSubmit({
                url:urlStr,
                type:"post",
                dataType:"json",
                success:function (data,status) {
                    alert("data:"+data.message)
                    if (data.code == 1){
                        setTimeout(function () {
                            window.location.href="/login"
                        },1000)
                    }
                },
                err:function (data,status) {
                    alert("err:"+data.message+":"+status)
                }
            })
        }
    })
})

 //登录
 $("#login-form").validate({
    rules:{
        username:{
            required:true,
            rangelength:[5,10]
        },
        password:{
            required:true,
            rangelength:[5,10]
        }
    },
    messages:{
        username:{
            required:"请输入用户名",
            rangelength:"用户名必须是5-10位"
        },
        password:{
            required:"请输入密码",
            rangelength:"密码必须是5-10位"
        }
    },
    submitHandler:function (form) {
        var urlStr ="/blog/login"
        alert("urlStr:"+urlStr)
        $(form).ajaxSubmit({
            url:urlStr,
            type:"post",
            dataType:"json",
            success:function (data,status) {
                alert("data:"+data.message+":"+status)
                if(data.code == 1){
                    setTimeout(function () {
                        window.location.href="/"
                    },1000)
                }
            },
            error:function (data,status) {
                alert("err:"+data.message+":"+status)
            }
        });
    }
});

//添加文章的表单
    $("#write-art-form").validate({
        rules: {
            title: "required",
            tags: "required",
            short: {
                required: true,
                minlength: 2
            },
            content: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            title: "请输入标题",
            tags: "请输入标签",
            short: {
                required: "请输入简介",
                minlength: "简介内容最少两个字符"
            },
            content: {
                required: "请输入文章内容",
                minlength: "文章内容最少两个字符"
            }
        },
        submitHandler: function (form) {
            var urlStr = "/blog/add";
            var artId = $("#write-article-id").val();
            if (artId > 0) {
                urlStr = "/blog/update/:"+artId
            }
            $(form).ajaxSubmit({
                url: urlStr,
                type: "post",
                dataType: "json",
                success: function (data, status) {
                    alert(":data:" + data.message);
                    setTimeout(function () {
                        window.location.href = "/"
                    }, 1000)
                },
                error: function (data, status) {
                    alert("err:" + data.message + ":" + status)
                }
            });
        }
    })